package com.fs.service.impl;


import com.fs.mapper.InventoryMapper;
import com.fs.mapper.OrderDetailsMapper;
import com.fs.mapper.OrderMapper;
import com.fs.pojo.Business;
import com.fs.pojo.Order;
import com.fs.pojo.OrderDetails;
import com.fs.pojo.ReturnToVendor;
import com.fs.service.BusinessService;
import com.fs.service.OrderService;
import com.fs.service.ReturnToService;
import com.fs.vo.Condition;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author hyy
 * @version 1.0
 * @description: 销售订单服务层
 * @date 2024/10/15 11:12
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private BusinessService businessService;

    @Override
    public List<Order> querylist(Condition<Order> condition) {
        return orderMapper.selectAll(condition.getSearch());
    }

    @Override
    public List<Order> querylistall(Condition<Order> condition) {
        return orderMapper.getOrderAll(condition.getSearch());
    }

    @Override
    public Order getOrderById(Integer id) {
        return orderMapper.getOrderById(id);
    }

    /**
     * 添加订单信息——修改销售库存信息
     * @param order
     * @return
     */
    @Transactional
    @Override
    public Boolean addOrder(Order order) {
        order.setCreateTime(new Date());
        order.setOrderStatus(0);
        Integer addOrder = orderMapper.addOrder(order);
        List<OrderDetails> orderDetails = order.getOrderDetails();
        Integer addOrderDetails=0;
        //添加订单详情信息
        for (OrderDetails orderDetail : orderDetails) {
            orderDetail.setOrderId(order.getOrderId());
           addOrderDetails = addOrderDetails+orderMapper.addOrderDetails(orderDetail);
        }
        return addOrder!=0&&addOrderDetails==orderDetails.size() ? true:false;
    }

    @Transactional
    @Override
    public Boolean delOrder(Integer id) {
        Integer i = orderMapper.delOrderDetails(id);
        Integer i1 = orderMapper.newDeleteById(id);
        return i.intValue()!=0&&i1.intValue()!=0?true:false;
    }

    /**
     * 修改订单信息——————0.未发货  、1.已完成 、2.退回（）修改销量库存信息
     * @param order
     * @return
     */
    @Transactional
    @Override
    public Boolean updateOrder(Order order) {
        System.out.println(order);
        int updats=0;
        double totalMoney=0;
        for (OrderDetails orderDetail : order.getOrderDetails()) {
            updats=updats+  orderDetailsMapper.updateById(orderDetail);
            totalMoney+=orderDetail.getTotalAmount();
        }
        if (order.getOrderStatus()==0){
            for (OrderDetails orderDetail : order.getOrderDetails()) {
            inventoryMapper.updateNumById(orderDetail.getProductId(),orderDetail.getTotal());
            }
        }
        else if (order.getOrderStatus() ==1) {
            order.setOrderDate(new Date());
            //修改销售库存
            for (OrderDetails orderDetail : order.getOrderDetails()) {
                inventoryMapper.updateNumById(orderDetail.getProductId(),orderDetail.getTotal());
            }
            businessService.addBusiness(new Business(order.getOrderId(),0,totalMoney,order.getOrderDate()));
        }
        int update = orderMapper.updateOrder(order);
        return update!=0&&updats!=0?true:false;
    }
}
